﻿Imports lbGlobalcom
Public Class fNotasCredito
    Dim iTipoDoc As cTipoDocumento
    Dim daNotaCredi As lbGlobalcom.dsMovimientoTableAdapters.MOV_MOVIMIENTOTableAdapter
    Dim daDescDeta As lbGlobalcom.dsMovimientoTableAdapters.MOV_DETALLENOTASCREDITOTableAdapter
    Dim daCli As lbGlobalcom.dsTerceroTableAdapters.TER_CLIENTESTableAdapter
    Private Sub MOV_MOVIMIENTOBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MOV_MOVIMIENTOBindingNavigatorSaveItem.Click
        Try
            salvaCambios(True)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub salvaCambios(ByVal msj As Boolean)
        Try
            Validate()
            If VerificarTotalNota() = True Then
                Dim cNotaCre As New lbGlobalcom.cNotasCredito(DsMovimiento, "", 0)
                MOV_MOVIMIENTOBindingSource.EndEdit()
                MOV_DETALLENOTASCREDITOBindingSource.EndEdit()
                daNotaCredi.Update(DsMovimiento.MOV_MOVIMIENTO)
                daDescDeta.Update(DsMovimiento.MOV_DETALLENOTASCREDITO)
                If msj Then
                    MessageBox.Show("Se salvarón registros con exito", "Salvar Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Else
                MessageBox.Show("Total Nota no es igual al valor del Documento", "Total Nota", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try

    End Sub

    Private Sub fNotasCredito_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If DsMovimiento.MOV_MOVIMIENTO.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    salvaCambios(True)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fNotasCredito_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daNotaCredi = New lbGlobalcom.dsMovimientoTableAdapters.MOV_MOVIMIENTOTableAdapter
            daDescDeta = New lbGlobalcom.dsMovimientoTableAdapters.MOV_DETALLENOTASCREDITOTableAdapter
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
            DsMovimiento.MOV_MOVIMIENTO.Columns("FECHADOCUMENTO").DefaultValue = Date.Today
            cGeneral.BloquearOrdenDatagrid(MOV_DETALLENOTASCREDITODataGridView)
            'DsMovimiento.MOV_MOVIMIENTO.Columns("IDDOCUMENTO").DefaultValue = ""
            'DsMovimiento.MOV_MOVIMIENTO.Columns("NUMERO").DefaultValue = 0
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Try
            If MessageBox.Show("Esta seguro que desea eliminar este registro", "Elimina Registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                MOV_MOVIMIENTOBindingSource.Remove(MOV_MOVIMIENTOBindingSource.Current)
                daNotaCredi.Update(DsMovimiento.MOV_MOVIMIENTO)
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub



    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
            If DsMovimiento.MOV_MOVIMIENTO.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    salvaCambios(True)
                End If
            End If
            MOV_MOVIMIENTOBindingSource.CancelEdit()
            MOV_DETALLENOTASCREDITOBindingSource.CancelEdit()
            DsMovimiento.MOV_DETALLENOTASCREDITO.Rows.Clear()
            DsMovimiento.MOV_MOVIMIENTO.Rows.Clear()
            MOV_MOVIMIENTOBindingSource.AddNew()
            txtValorDocumento.Enabled = False
            VALORBASETextBox.Text = 0
            VALORIVATextBox.Text = 0
            txtValorDocumento.Text = 0
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaDocumento_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaDocumento.Click
        Try
            Dim fLDoc As New fListaTipoDocs
            fLDoc.SubClase = "NOC"
            If fLDoc.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDDOCUMENTOTextBox.Text = fLDoc.idTipoDoc
                CargaTipoDoc(fLDoc.idTipoDoc)
                IDCCOSTOTextBox.Text = fLDoc.Ccosto
                NUMEROTextBox.Focus()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub CargaTipoDoc(ByVal TipoDoc As String)
        Try
            iTipoDoc = New cTipoDocumento(TipoDoc)
            If iTipoDoc Is Nothing Then
                MessageBox.Show("El tipo de documento seleccionado no esta registrado", "Error en tipo de documento", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else
                NUMEROTextBox.Text = lbGlobalcom.Consec.GetNumeroActual(TipoDoc, iTipoDoc.DocEs)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaTercero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTercero.Click
        Try
            Dim fBT As New fBuscaTercero
            fBT.TipoTercero = "Cliente"
            daCli = New lbGlobalcom.dsTerceroTableAdapters.TER_CLIENTESTableAdapter
            If fBT.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDTERCEROTextBox.Text = fBT.midTercero
                SUCURSALTextBox.Text = fBT.mSucursal
                IDCANALTextBox.Text = daCli.CLI_CANAL(fBT.midTercero, fBT.mSucursal)
                IDSEDETextBox.Focus()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    'Private Sub IDDOCUMENTOTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles IDDOCUMENTOTextBox.TextChanged

    '    End Sub

    Private Sub btnBuscaCanales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCanales.Click
        Try
            Dim flCan As New fListaCanales
            If flCan.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDCANALTextBox.Text = flCan.Canal
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub btnBuscaSedes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaSedes.Click
        Try
            Dim flsede As New fListaSedes
            If flsede.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDSEDETextBox.Text = flsede.Sede
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    'Private Sub VALORBASETextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles VALORBASETextBox.TextChanged

    'End Sub

    'Private Sub VALORIVATextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles VALORIVATextBox.TextChanged

    'End Sub

    Private Sub VALORBASETextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles VALORBASETextBox.Validated
        Try
            If VALORBASETextBox IsNot Nothing AndAlso VALORBASETextBox.Text <> "" Then
                If IDDOCUMENTOTextBox.Text <> "" AndAlso NUMEROTextBox.Text <> "0" Then
                    calcularTotal()
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub calcularTotal()
        Try
            txtValorDocumento.Text = CDbl(VALORBASETextBox.Text) + CDbl(VALORIVATextBox.Text)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub VALORIVATextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles VALORIVATextBox.Validated
        Try
            If VALORIVATextBox IsNot Nothing AndAlso VALORIVATextBox.Text <> "" Then
                If IDDOCUMENTOTextBox.Text <> "" AndAlso NUMEROTextBox.Text <> "0" Then
                    calcularTotal()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaNotasCredito_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaNotasCredito.Click
        Try
            Dim flNotaCre As New fListaNotasCredito
            If flNotaCre.ShowDialog = Windows.Forms.DialogResult.OK Then
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
                txtValorDocumento.Enabled = False
                IDDOCUMENTOTextBox.Text = flNotaCre.pDocumento
                NUMEROTextBox.Text = flNotaCre.pNumero
                daNotaCredi.FillByIdDocumentoNumero(DsMovimiento.MOV_MOVIMIENTO, flNotaCre.pDocumento, flNotaCre.pNumero)
                daDescDeta.FillByIdDocumento(DsMovimiento.MOV_DETALLENOTASCREDITO, flNotaCre.pDocumento, flNotaCre.pNumero)
                calcularTotal()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub IDDOCUMENTOTextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles IDDOCUMENTOTextBox.Validated
        Try
            If IDDOCUMENTOTextBox.Text.Trim.Length > 0 Then
                iTipoDoc = New cTipoDocumento(IDDOCUMENTOTextBox.Text)
                'CargaTipoDoc(IDDOCUMENTOTextBox.Text)
                'IDCCOSTOTextBox.Text = iTipoDoc.idCcosto
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub MOV_DETALLENOTASCREDITODataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles MOV_DETALLENOTASCREDITODataGridView.CellBeginEdit
        Try
            MOV_DETALLENOTASCREDITODataGridView.Rows(e.RowIndex).Cells("NUMEROLINEA").Value = cGeneral.GetNumeroLinea(MOV_DETALLENOTASCREDITODataGridView, e.RowIndex, "NUMEROLINEA")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub MOV_DETALLENOTASCREDITODataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles MOV_DETALLENOTASCREDITODataGridView.CellValidating
        Try
            If MOV_DETALLENOTASCREDITODataGridView.IsCurrentCellInEditMode AndAlso e.FormattedValue IsNot Nothing AndAlso e.FormattedValue <> "" Then
                Select Case MOV_DETALLENOTASCREDITODataGridView.Columns(e.ColumnIndex).Name
                    Case "VALORNOTA"
                        If e.FormattedValue < 0 Then
                            MessageBox.Show("Valor debe ser mayor de cero", "Nota", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                            e.Cancel = True
                        End If
                        If e.FormattedValue > (CDbl(VALORBASETextBox.Text) + CDbl(VALORIVATextBox.Text)) Then
                            MessageBox.Show("Valor de la nota debe ser menor", "Nota", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                            e.Cancel = True
                        End If
                        Dim valorNota As Decimal = cGeneral.GetTotalNotaCredito(DsMovimiento.MOV_DETALLENOTASCREDITO)
                        If valorNota <> 0 And (valorNota + e.FormattedValue) > (CDbl(VALORBASETextBox.Text) + CDbl(VALORIVATextBox.Text)) Then
                            MessageBox.Show("Valor de la nota debe ser menor", "Nota", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                            e.Cancel = True
                        End If
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try

    End Sub

    Private Sub MOV_DETALLENOTASCREDITODataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles MOV_DETALLENOTASCREDITODataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub

    Private Sub MOV_DETALLENOTASCREDITODataGridView_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MOV_DETALLENOTASCREDITODataGridView.GotFocus
        Try
            MOV_MOVIMIENTOBindingSource.EndEdit()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub MOV_DETALLENOTASCREDITODataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MOV_DETALLENOTASCREDITODataGridView.KeyDown
        Try
            If e.Control Then
                Select Case e.KeyCode
                    Case Keys.B
                        If MOV_DETALLENOTASCREDITODataGridView.CurrentCellAddress.X = 3 Then
                            Dim fLMoviFac As New fListaMoviFacturado
                            fLMoviFac.mTercero = Me.IDTERCEROTextBox.Text
                            fLMoviFac.mSucursal = Me.SUCURSALTextBox.Text
                            If fLMoviFac.ShowDialog = Windows.Forms.DialogResult.OK Then
                                MOV_DETALLENOTASCREDITODataGridView.BeginEdit(True)
                                MOV_DETALLENOTASCREDITODataGridView.Rows(MOV_DETALLENOTASCREDITODataGridView.CurrentRow.Index).Cells(3).Value = fLMoviFac.mTipoDoc
                                MOV_DETALLENOTASCREDITODataGridView.Rows(MOV_DETALLENOTASCREDITODataGridView.CurrentRow.Index).Cells(4).Value = fLMoviFac.mNumeroDoc
                                'MOV_DETALLENOTASCREDITODataGridView.Rows(MOV_DETALLENOTASCREDITODataGridView.CurrentRow.Index).Cells(5).Value = fLMoviFac.mProducto
                                'MOV_DETALLENOTASCREDITODataGridView.Rows(MOV_DETALLENOTASCREDITODataGridView.CurrentRow.Index).Cells(6).Value = fLMoviFac.mImei
                                'MOV_DETALLENOTASCREDITODataGridView.Rows(MOV_DETALLENOTASCREDITODataGridView.CurrentRow.Index).Cells(7).Value = fLMoviFac.mIccid
                                MOV_DETALLENOTASCREDITODataGridView.Rows(MOV_DETALLENOTASCREDITODataGridView.CurrentRow.Index).Cells(5).Value = 0
                            End If
                        End If
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try

    End Sub

    Private Function VerificarTotalNota() As Boolean
        Dim TotalPagos As Double = 0
        For Each drMov As dsMovimiento.MOV_DETALLENOTASCREDITORow In DsMovimiento.MOV_DETALLENOTASCREDITO.Rows
            TotalPagos += drMov.VALORNOTA
        Next
        If TotalPagos = CType(txtValorDocumento.Text, Double) Then
            'DsMovimiento.MOV_MOVIMIENTO.
            Return True
        Else
            Return False
        End If
    End Function

End Class